package cn.chencq.moudle.sys.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.chencq.moudle.sys.dao.BaseDao;
import cn.chencq.moudle.sys.dao.SysRoleMenuDao;
import cn.chencq.moudle.sys.entity.SysRoleMenu;
import cn.chencq.moudle.sys.service.SysRoleMenuService;

@Service
public class SysRoleMenuServiceImpl extends BaseServiceImpl<SysRoleMenu> implements SysRoleMenuService {

	@Autowired
	private SysRoleMenuDao sysRoleMenuDao;

	@Override
	protected BaseDao getBaseDao() {
		return sysRoleMenuDao;
	}

	@Override
	public List<Long> queryMenuIdList(Long roleId) {
		return sysRoleMenuDao.queryMenuIdList(roleId);
	}

	@Override
	@Transactional
	public void saveOrUpdate(Long roleId, List<Long> menuIdList) {
		// 先删除角色与菜单关系
		sysRoleMenuDao.deleteByRoleId(roleId);

		if (menuIdList.size() == 0) {
			return;
		}

		// 保存角色与菜单关系
		List<SysRoleMenu> list = new ArrayList<>();
		for (Long menuId : menuIdList) {
			SysRoleMenu sysRoleMenu = new SysRoleMenu();
			sysRoleMenu.setMenuId(menuId);
			sysRoleMenu.setRoleId(roleId);
			list.add(sysRoleMenu);
		}
		sysRoleMenuDao.batchSave(list);
	}
}
